/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LookAndFeelJDialog.java
 *
 * Created on 2010-12-17, 13:51:22
 */
package org.lifxue.jqda.ui;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lifxue.jqda.data.DataUtil;
import org.lifxue.jqda.util.GlobalConstant;
import org.lifxue.jqda.util.GlobalVar;
import org.lifxue.jqda.ui.util.UIUtil;
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.SubstanceSkin;
import org.pushingpixels.substance.api.skin.SkinInfo;

/**
 *
 * @author lifxue
 */
public class LookAndFeelJDialog extends javax.swing.JDialog {

    private static Log logger = LogFactory.getLog(LookAndFeelJDialog.class);
    private static LookAndFeelJDialog LAF_JDIALOG;

    public static LookAndFeelJDialog getInstance(boolean modal) {
        synchronized (LookAndFeelJDialog.class) {
            if (LAF_JDIALOG == null) {
                LAF_JDIALOG = new LookAndFeelJDialog(modal);
            }
        }
        return LAF_JDIALOG;
    }

    public LookAndFeelJDialog(boolean modal) {
        super(new javax.swing.JFrame(), modal);
        initComponents();

        //窗口定位
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = getSize();
        setLocation(new Point((screenSize.width - frameSize.width) / 2,
                (screenSize.height - frameSize.height) / 2));

    }

    /** Creates new form LookAndFeelJDialog
     * @param parent
     * @param modal 
     */
    public LookAndFeelJDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("界面风格设置");
        setIconImage(Toolkit.getDefaultToolkit().createImage(getClass().getResource("/org/lifxue/jqda/ui/butterfly16.png")));
        setResizable(false);

        addComBoxItem();
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        jLabel1.setText("选择界面风格:");

        jButton1.setText("关闭");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton1))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        dispose();

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        // TODO add your handling code here:
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            String lafName = evt.getItem().toString();
            setUI(lafName);
        }
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void addComBoxItem() {
        String LookAndFeelDefault = "";
        SubstanceSkin currentSkin = SubstanceLookAndFeel.getCurrentSkin();
        if (currentSkin != null) {
            LookAndFeelDefault = currentSkin.getDisplayName();
        }

        //substance
        Map<String, SkinInfo> hm = SubstanceLookAndFeel.getAllSkins();
        Iterator skins = hm.entrySet().iterator();
        while (skins.hasNext()) {
            java.util.Map.Entry entry = (java.util.Map.Entry) skins.next();
            String key = (String) entry.getKey(); //返回与此项对应的键
            SkinInfo in = (SkinInfo) entry.getValue();
            //logger.info(in.getClassName());
            jComboBox1.addItem(key);
            if (LookAndFeelDefault.equals(key)) {
                jComboBox1.setSelectedItem(key);
            }
            SkinInfo skinInfo = (SkinInfo) entry.getValue();
        }

    }

    private void setUI(String lafName) {
        //sunstance
        Map<String, SkinInfo> hm = SubstanceLookAndFeel.getAllSkins();
        Iterator skins = hm.entrySet().iterator();
        while (skins.hasNext()) {
            java.util.Map.Entry entry = (java.util.Map.Entry) skins.next();
            SkinInfo skinInfo = (SkinInfo) entry.getValue();

            if (lafName.equals(skinInfo.getDisplayName())) {
                GlobalVar.CONFIG.put(GlobalConstant.CONFIG_LOOK_AND_FEEL, skinInfo.getClassName());
                //保存界面风格数据到文件
                DataUtil.updateConfigFile();
                UIUtil.setUI();
                return;
            }
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                LookAndFeelJDialog dialog = new LookAndFeelJDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}
